<script type="text/javascript">
	$(document).ready(function(){
		

		var form_action = "<?php echo $action; ?>";
		
		if(form_action == "add"){
			//$("#name_label,#login_label,#password_label,#confirm_password_label,#email_label,#surname_label").addClass("required");
		}else if(form_action == "edit"){
			$("#login_label").addClass("required");
			$("#login_name").attr("disabled","disabled");
			
			$("#user_id").val("<?php echo isset($user)?$user[0]->userid:""; ?>");
			$("#name").val("<?php echo isset($user)?$user[0]->name:""; ?>");
			$("#surname").val("<?php echo isset($user)?$user[0]->surname:""; ?>");
			$("#login_name").val("<?php echo isset($user)?$user[0]->loginname:""; ?>");
			$("#role").val("<?php echo isset($user)?$user[0]->role:""; ?>");
			$("#email").val("<?php echo isset($user)?$user[0]->email:""; ?>");			
			$("#password,#confirm_password").val("");
			$enabled = "<?php echo isset($user)?$user[0]->enabled:""; ?>";
			if($enabled == "1"){
				$("#user_enabled").attr("checked","checked");				
			}else{
				$("#user_enabled").removeAttr("checked");
			}
		}else{
			alert("Operation Invalid.");
		}

		$("#save").on('click', function (event) {
			
			if(isValid()){
			
			var url = "<?php echo base_url(); ?>index.php/user/";

			if (form_action == "add"){
				url += "doCreate";
			}else if(form_action == "edit"){
				url += "doEdit";
			}

			 var form_data = $('form').serialize();
			
			var request = $.ajax({
				url: url,
				type: "POST",			
			    async: false,
		        dataType: "html",
		        data: form_data
			});

			request.done(function(msg) {				
				if (form_action == "add"){
					$("#team_name").val("");
					$("#team_enabled").attr("checked","checked");			
				}			
				alert("Data Saved");		
			});

			request.fail(function(jqXHR, textStatus) {
				alert( "Request failed: " + textStatus );
			 });
		   }
		});
		

		$("#back").on('click', function (event) {
			window.location = "<?php echo base_url(); ?>index.php/user";
		});

		function isValid(){
			$password = $("#password").val();
			$confirm_password = $("#confirm_password").val();
			valid = true;
			
			if($password != $confirm_password ){
				valid = false;
				$("#message").text("Password not match.");
			}else if(($("#login_name").val().length < 6 || $("#password").val().length < 6) && form_action != "edit"){
				valid = false;
				$("#message").text("Login name or Password must at lease 6 character.");			
			}else if($("#login_name").val() == ""){
				valid = false;
				$("#message").text("Login name is required.");	
			}else if($("#password").val() == ""  && form_action != "edit"){
				valid = false;
				$("#message").text("Password is required.");	
			}else if($("#email").val() == ""){
				valid = false;
				$("#message").text("Email is required.");	
			}else if($("#name").val() == ""){
				valid = false;
				$("#message").text("Name is required.");	
			}else if($("#surname").val() == ""){
				valid = false;
				$("#message").text("Surname is required.");	
			}

			if(!valid){
				$("#message").dialog({modal: true});
			}
			return valid;
		}
		
	});
</script>
<img id="top" src="top.png" alt="">
<div id="form_container">


	<form id="form_889069" class="appnitro" method="post" action="">
		<div class="form_description">
			<h2><?php echo $title; ?></h2>
		</div>
		<ul>

			<li id="li_1"><label class="description" id="login_label">Login Name</label>
				<div>
					<input id="login_name" name="login_name" class="element text small"
						type="text" maxlength="20" value="" />
				</div></li>
			<li id="li_1"><label class="description" id="password_label">Password</label>
				<div>
					<input id="password" name="password" class="element text medium"
						type="password" maxlength="20" value="" />
				</div></li>
			<li id="li_1"><label class="description" id="confirm_password_label">Confirm
					Password</label>
				<div>
					<input id="confirm_password" name="confirm_password"
						class="element text medium" type="password" maxlength="20"
						value="" />
				</div></li>
			<li id="li_2"><label class="description" id="name_label">Name</label>
				<div>
					<input id="name" name="name" class="element text medium"
						type="text" maxlength="100" value="" />
				</div></li>
			<li id="li_2"><label class="description" id="surname_label">Surname</label>
				<div>
					<input id="surname" name="surname" class="element text medium"
						type="text" maxlength="100" value="" />
				</div></li>
			<li id="li_2"><label class="description" id="email_label">Email</label>
				<div>
					<input id="email" name="email" class="element text medium"
						type="text" maxlength="20" value="" />
				</div></li>
			<li id="li_2"><label class="description" id="type_label">Role</label>
				<div>
					<select id="role" name="role">
						<option value="Admin" selected>Admin</option>
						<option value="Guest">Guest</option>
					</select>
				</div></li>
			<li id="li_3"><label class="description" for="element_3"> </label> <span>
					<input id="user_enabled" name="user_enabled" class="element checkbox "
					type="checkbox" value="1" checked="checked" /> <label
					class="choice" for="element_3_1">Enabled</label>

			</span></li>

			<li class="buttons"><input type="hidden" id="user_id" name="user_id"
				value="" /> <input type="button" value="Save" id="save"
				class="button_text" /> <input type="button" value="Back" id="back"
				class="button_text" /></li>
		</ul>
	</form>
</div>
